import { scene } from '../scene/index'
import Cuboid from '../block/cuboid'
import Cylinder from '../block/cylinder'
import ground from '../objects/ground'
import bottle from '../objects/bottle'

export default class GamePage {
  constructor (callbacks) {
    this.callbacks = callbacks
  }
  init () {
    console.log('init')
    this.scene = scene
    this.ground = ground
    this.bottle = bottle
    this.scene.init()
    this.ground.init()
    this.bottle.init()
    
    this.addInitBlock()
    this.addGround()
    this.addBottle()
    this.bindTouchEvent()
    this.render()
  }
  bindTouchEvent () {
    canvas.addEventListener('touchstart', this.touchStartCallback)
    canvas.addEventListener('touchend', this.touchEndCallback)
  }
  removeTouchEvent () {
    canvas.removeTEventListener('touchstart', this.touchStartCallback)
    canvas.removeTEventListener('touchend', this.touchEndCallback)
  }
  touchStartCallback = () => {
    this.bottle.shrink()
    console.log('touchStartCallback')
  }
  touchEndCallback = () => {
    this.bottle.stop()
    this.bottle.rotate()
    console.log('touchEndCallback')
  }
  render () {
    this.scene.render()
    if (this.bottle) {
      this.bottle.update()
    }
    requestAnimationFrame(this.render.bind(this))
  }
  addInitBlock () {
    this.cuboidBlock = new Cuboid(-15, 0, 0) 
    this.scene.instance.add(this.cuboidBlock.instance)
    this.cylinderBlock = new Cylinder(23, 0, 0)
    this.scene.instance.add(this.cylinderBlock.instance)
  }
  addGround () {
    this.scene.instance.add(this.ground.instance)
    this.bottle.showup()
  }
  addBottle () {
    this.scene.instance.add(this.bottle.obj)
  }
  show () {
  }
  hide () {
  }
  restart () {
    console.log('game page restart')
  }
}